var grid;
var opeForm;

$(function() {
	opeForm = $('#opeForm');
	//$('input[type="button"]').button();

	grid = $('#gridTable').jqGrid({
		caption : 'Certificate Association Mangement',
		datatype : 'local',
		mtype : 'post',
		height : '400',
		autowidth : true, 
		colModel : [ {
			name : 'CN',
			label : 'cn',
			sortable:false,
			hidden : true
		},{
			name : 'STATUS',
			label : 'Status',
			sortable:false
		}, {
			name : 'BD_UID',
			label : 'User id',
			sortable:false
		}, {
			name : 'BD_USER_NAME',
			label : 'User name',
			sortable:false
		}, {
			name : 'CER_NAME',
			label : 'Cert Name',
			sortable:false
		}, {
			name : 'VALIDITY_END_DATE',
			label : 'Expiry date',
			sortable:false
		},{
			name : 'LAST_UPDATE',
			label : 'Last update',
			sortable:false
		},],
		viewrecords : true,
		multiselect : false,
		rownumbers: true,
		rownumWidth: 40,
		rowNum: '-1',	// disable paging
		prmNames : {
			page : 'pageNum', // 表示请求页码的参数名称
			rows : 'pageSize' // 表示请求行数的参数名称
		},
		jsonReader : {
			root : 'rows', // json中代表实际模型数据的入口
			page : 'pageNum', // json中代表当前页码的数据
			total : 'totalPage', // json中代表页码总数的数据
			records : 'totalNum', // json中代表数据行总数的数据
			repeatitems : false
		}
	});

	queryOperation();
	$('#queryBtn').click(queryOperation);
	$('#clearBtn').click(clearOperation);
	$('#activateBtn').click(activateOperation);
	$('#deactivateBtn').click(deactivateOperation);
	$('#preRegCertBtn').click(preRegCertOperation);
	$('#exportCSVBtn').click(exportCSVOperation);
	$('#deleteBtn').click(deleteOperation);
	$('#viewBtn').click(viewOperation);

});

function queryOperation() {
	var params = opeForm.serializeObject();
	grid.jqGrid('setGridParam', {
		url : 'CER_ASSOC_MANAGE!queryList.do',
		datatype : 'json',
		postData : jQuery.extend({}, params)
	}).trigger('reloadGrid');
}

function clearOperation() {
	opeForm.resetForm();
}

function activateOperation() {
	var id = grid.jqGrid('getGridParam', 'selrow');
	if (id == null) {
		alert('Please select a record!');
		return;
	}
	
	var row = grid.jqGrid('getRowData', id);
	var url = 'CER_ASSOC_MANAGE!showActivateOrDeactivate.do?operationType=4&model.CN=' + row.CN;
	openWin(url, 'Detail page');
	
}

function deactivateOperation() {
	var id = grid.jqGrid('getGridParam', 'selrow');
	if (id == null) {
		alert('Please select a record!');
		return;
	}
	var row = grid.jqGrid('getRowData', id);
	var url = 'CER_ASSOC_MANAGE!showActivateOrDeactivate.do?operationType=5&model.CN=' + row.CN;
	openWin(url, 'Detail page');
	
}

function preRegCertOperation() {
	var url = 'CER_ASSOC_MANAGE!showPreReg.do';
	openWin(url, 'Detail page');
	
}

function exportCSVOperation() {
	var url = 'CER_ASSOC_MANAGE!doExportCSV.do';
	window.open(url);
	
}

function deleteOperation() {
	var id = grid.jqGrid('getGridParam', 'selrow');
	if (id == null) {
		alert('Please select a record!');
		return;
	}
	var row = grid.jqGrid('getRowData', id);
	var params = {'model.CN' : row.CN};

	if (confirm('Are you confirm to delete selected record?')) {
		$.ajax({
			url : 'CER_ASSOC_MANAGE!doDelete.do',
			data : params,
			type : 'post',
			success : function(data) {
				alert('Operation successfull!');
				afterSave();
			}
		});
	}
}

function viewOperation() {
	var id = grid.jqGrid('getGridParam', 'selrow');
	if (id == null) {
		alert('Please select a record!');
		return;
	}
	var row = grid.jqGrid('getRowData', id);
	var url = 'CER_ASSOC_MANAGE!showInfo.do?model.CN=' + row.CN;
	openWin(url, 'Detail page');
}

function afterSave() {
	queryOperation();
}
